package com.jwthhealth_pub.old.report.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.SportSuggListResp;
import com.jwthhealth_pub.old.report.view.SportSuggestActivity;
import com.jwthhealth_pub.old.report.view.widget.Jwth_IndicatorLine;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 运动阶段导航（第几天）
 * Created by liuzhen on 2017/10/24.
 */

public class SportStepNavAdapter extends RecyclerView.Adapter {
    private final SportSuggestActivity mActivity;
    private final List<SportSuggListResp.DataBeanX.DayListBean.DataBean> mData;
    private SportsuggDayPlanAdapter sportsuggDayPlanAdapter;
    private int mCurrentStage;// 适配器当前适配的是哪一阶段
    private int mSelDay = -1;//已选择的位置
    private int mSelStage;//已选择的阶段

    SportStepNavAdapter(SportSuggestActivity mActivity, SportsuggDayPlanAdapter sportsuggDayPlanAdapter, List<SportSuggListResp.DataBeanX.DayListBean.DataBean> contentList, int mCurrentStage, int mSelStage, int mSelDay) {
        this.mActivity = mActivity;
        this.sportsuggDayPlanAdapter = sportsuggDayPlanAdapter;
        this.mData = contentList;
        this.mCurrentStage = mCurrentStage;
        this.mSelStage = mSelStage;
        this.mSelDay = mSelDay;
    }

    void setSelDay(int mSelDay) {
        this.mSelDay = mSelDay;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mActivity, R.layout.item_sport_suggest_nav, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder vh = (ViewHolder) holder;

        if (mSelStage != mCurrentStage) {
            vh.lineSelect.setVisibility(View.GONE);
            vh.lineNormal.setVisibility(View.GONE);
        } else if (vh.getAdapterPosition() == mSelDay) {
            vh.lineSelect.setVisibility(View.VISIBLE);
            vh.lineNormal.setVisibility(View.GONE);
        } else {
            vh.lineSelect.setVisibility(View.GONE);
            vh.lineNormal.setVisibility(View.VISIBLE);
        }


        SportSuggListResp.DataBeanX.DayListBean.DataBean dataBean = mData.get(vh.getAdapterPosition());

        vh.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断阶段
                if (mCurrentStage != mSelStage) {
                    //阶段切换，清除默认位置
                    mSelDay = vh.getAdapterPosition();
                    notifyDataSetChanged();
//                    mActivity.showStageLayout(mCurrentStage, position);

                    if (mSelDay >= 0)
                        sportsuggDayPlanAdapter.changeDayPlan(mCurrentStage, mSelDay);

                } else {
                    //相同阶段 判断天数（位置）
                    if (mSelDay != vh.getAdapterPosition()) {
                        mSelDay = vh.getAdapterPosition();

                        if (mSelDay == -1)
                            mSelDay = vh.getAdapterPosition();

                        notifyDataSetChanged();

                        if (mSelDay >= 0)
                            sportsuggDayPlanAdapter.changeDayPlan(mCurrentStage, mSelDay);
                    }
                }
            }
        });


        if (dataBean == null)
            return;

        vh.title.setText(dataBean.getTitle());

        if (mSelStage == mCurrentStage && mSelDay == vh.getAdapterPosition()) {
            vh.title.setTextColor(mActivity.getResources().getColor(R.color.primarycolor));
            vh.desc.setTextColor(mActivity.getResources().getColor(R.color.primarycolor));
        } else {
            vh.title.setTextColor(mActivity.getResources().getColor(R.color.textColorBlack));
            vh.desc.setTextColor(mActivity.getResources().getColor(R.color.textColorBlack));
        }

        if (dataBean.getDay() != null) {
            vh.desc.setText(dataBean.getDay());
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.line_select)
        Jwth_IndicatorLine lineSelect;
        @BindView(R.id.line_normal)
        View lineNormal;
        @BindView(R.id.click_layout)
        LinearLayout clickLayout;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.desc)
        TextView desc;

        public ViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);

            clickLayout = itemView.findViewById(R.id.click_layout);
            title = itemView.findViewById(R.id.title);
            desc = itemView.findViewById(R.id.desc);
        }
    }
}

